package wx.core
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	/**
	 * 
	 * @author wujianqi
	 * 
	 * 组件 群组	 
	 * 
	 * 使用项呈现器（IItemReader）
	 * 
	 */	
	[Event(name="change",type="flash.events.Event")]
	[Event(name="added",type="flash.events.Event")]
	[Event(name="removed",type="flash.events.Event")]
	
	public class ListCollection extends EventDispatcher
	{
		
		private var _items:Vector.<IItemReader>;
		private var _selectItem:IItemReader;
		
		public function ListCollection(items:Vector.<IItemReader> = null)
		{
			_items = items ? items : new <IItemReader>[] ;
		}
		
		public function get selectItem():IItemReader
		{
			return _selectItem;
		}

		public function set selectItem(value:IItemReader):void
		{
			_selectItem = value;
			dispatchEvent(new Event(Event.CHANGE));
		}

		/**
		 * 
		 * @param item
		 * 添加可重复项
		 */		
		public function addItem(item:IItemReader):void{
			_items.push(item);
			dispatchEvent(new Event(Event.ADDED));
		}
		
		/**
		 * 
		 * @param item
		 * 添加不可重复项
		 */		
		public function addNew(item:IItemReader):void{
			if(_items.indexOf(item)==-1) _items.push(item);
			dispatchEvent(new Event(Event.ADDED));
		}
		
		public function setItemAt(item:IItemReader,index:int):void{
			_items.splice(index,0,item);
			dispatchEvent(new Event(Event.ADDED));
		}
		
		public function get items():Vector.<IItemReader>{
			return _items;
		}
		
		public function getItemAt(index:int):IItemReader{
			return _items[index];
		}
		
		public function getItemIndex(item:IItemReader):int{
			return _items.indexOf(item);
		}
		
		public function removeItem(item:IItemReader):void{
			_items.splice( _items.indexOf(item),1);
			dispatchEvent(new Event(Event.REMOVED));
		}
		
		public function removeItemAt(index:int):void{
			_items.splice(index,1);
			dispatchEvent(new Event(Event.REMOVED));
		}
		
		public function removeItems():void{
			_items = null;
			dispatchEvent(new Event(Event.REMOVED));
		}
		
		
		
	}
}